<template>
  <view class="privacy-settings-modal">
    <up-popup v-model:show="show" overlay mode="center" :round="10" closeable closeOnClickOverlay
      @close="handleCancel" @open="open">
      <view class="modal-content">
        <view class="modal-header">
          <view class="modal-title">车辆隐私保护设置</view>
        </view>
        <view class="modal-body">
          <view class="setting-item">
            <view class="setting-label">禁止他人绑定</view>
            <view class="setting-control">
              <up-radio-group v-model="settings.disallowBinding" direction="horizontal">
                <up-radio name="true">是</up-radio>
                <up-radio name="false">否</up-radio>
              </up-radio-group>
            </view>
          </view>
          <view class="setting-item">
            <view class="setting-label">禁止他人查询欠费</view>
            <view class="setting-control">
              <up-radio-group v-model="settings.disallowDebtQuery" direction="horizontal">
                <up-radio name="true">是</up-radio>
                <up-radio name="false">否</up-radio>
              </up-radio-group>
            </view>
          </view>
          <view class="setting-item">
            <view class="setting-label">不推送车入消息给绑定用户</view>
            <view class="setting-control">
              <up-radio-group v-model="settings.disallowEntryNotification" direction="horizontal">
                <up-radio name="true">是</up-radio>
                <up-radio name="false">否</up-radio>
              </up-radio-group>
            </view>
          </view>
        </view>
        <view class="modal-footer">
          <up-button plain @click="handleCancel">取消</up-button>
          <up-button type="primary" @click="handleSave">保存</up-button>
        </view>
      </view>
    </up-popup>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive, defineProps, defineEmits, defineExpose, watch } from 'vue';

defineOptions({ name: "PrivacySettingsModal" });

// 定义属性
const props = defineProps({
  initialSettings: {
    type: Object,
    default: () => ({
      disallowBinding: 'false',
      disallowDebtQuery: 'false',
      disallowEntryNotification: 'false'
    })
  }
});

// 定义事件
const emit = defineEmits(['update:show', 'save', 'cancel']);

// 创建响应式数据  
const show = defineModel<boolean>("show", { default: false });
const settings = reactive({
  disallowBinding: 'false',
  disallowDebtQuery: 'false',
  disallowEntryNotification: 'false'
});

// 监听初始设置变化
watch(() => props.initialSettings, (newVal) => {
  if (newVal) {
    Object.assign(settings, newVal);
  }
}, { immediate: true, deep: true });

// 打开弹窗
function open() {
  show.value = true;
  // 重置为初始值
  Object.assign(settings, props.initialSettings);
}

// 保存设置
function handleSave() {
  emit('save', { ...settings });
  show.value = false;
}

// 取消操作
function handleCancel() {
  emit('cancel');
  show.value = false;
}

// 暴露方法
defineExpose({
  open
});
</script>

<style lang="scss" scoped>
.privacy-settings-modal {
  .modal-content {
    width: 90vw;
    background-color: #fff;
    border-radius: 8px;
    padding: 20px;
    
    .modal-header {
      margin-bottom: 20px;
      
      .modal-title {
        font-size: 16px;
        font-weight: bold;
        text-align: center;
        color: #333;
      }
    }
    
    .modal-body {
      padding: 10px 0;
      
      .setting-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px 0;
        border-bottom: 1px solid #eee;
        
        &:last-child {
          border-bottom: none;
        }
        
        .setting-label {
          font-size: 14px;
          color: #333;
          flex: 1;
        }
        
        .setting-control {
          flex: 1;
          display: flex;
          justify-content: flex-end;
        }
      }
    }
    
    .modal-footer {
      margin-top: 20px;
      display: flex;
      justify-content: space-around;
      
      .up-button {
        min-width: 100px;
      }
    }
  }
}
</style>